package com.zj.joke.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import com.zj.joke.form.RemoteItem;

public class HtmlParser {
	public static ArrayList<RemoteItem> getMyObjects(String url) {
		ArrayList<RemoteItem> myObjects = new ArrayList<RemoteItem>();
		try {
			Document document = null;
			document = Jsoup.connect(url).get();
			// 找到对应的文章
			Elements myObjeElements = document.select("li.article-summary")
					.select("li:has(span.article-title)")
					.select("li:has(div.summary-text");
			for (Element myObjElement : myObjeElements) {
				RemoteItem myObject = new RemoteItem();
				Element titleElement = myObjElement
						.select("span.article-title").get(0);
				myObject.setTitle(titleElement.text());
				Element contentElement = myObjElement
						.select("div.summary-text").get(0);
				List<TextNode> nodes = contentElement.textNodes();
				StringBuffer buffer = new StringBuffer();
				for (TextNode node : nodes) {
					buffer.append(node.text());
					buffer.append("\r\n");
				}
				if (buffer.length() >= 2) {
					buffer = buffer
							.delete(buffer.length() - 2, buffer.length());
				}
				myObject.setContent(buffer.toString());
				myObjects.add(myObject);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return myObjects;
	}
}
